{% embed 'Embed/table_embed.html.twig' %}
    {% import 'Import/app_import.html.twig' as appImport %}

    {% set percentThreshold = 90 %}

    {% set columns = {'name': true, 'uptime': true, 'load_average': false, 'cpu': true, 'disk': true, 'memory': true, 'heap': true, 'file_descriptors': true} %}

    {% if true == hasFeature('load_average') %}
        {% set columns = columns|merge({'load_average': true}) %}
    {% endif %}

    {% block thead %}
        <tr>
            {% for column, display in columns %}
                {% if true == display %}
                    {% if 'name' == column %}
                        <th class="text-nowrap">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'name', 'title': 'name'|trans, 'default': ':asc'})}}
                        </th>
                    {% endif %}

                    {% if 'cpu' == column %}
                        <th class="text-nowrap">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'cpu', 'title': 'cpu'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'heap' == column %}
                        <th class="text-nowrap d-none d-xl-table-cell">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'heap.percent', 'title': 'heap'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'memory' == column %}
                        <th class="text-nowrap">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'ram.percent', 'title': 'memory'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'disk' == column %}
                        <th class="text-nowrap">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'disk.used_percent', 'title': 'disk'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'uptime' == column %}
                        <th class="text-nowrap d-none d-xl-table-cell">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'uptime', 'title': 'uptime'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'load_average' == column %}
                        <th class="text-nowrap">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'load_1m', 'title': 'load_average'|trans})}}
                        </th>
                    {% endif %}

                    {% if 'file_descriptors' == column %}
                        <th class="text-nowrap d-none d-xl-table-cell">
                            {{ appImport.sort({'cat_sort': cat_sort, 'url': path('nodes'), 'sort': 'file_desc.percent', 'title': 'file_descriptors'|trans})}}
                        </th>
                    {% endif %}
                {% endif %}
            {% endfor %}
        </tr>
    {% endblock %}

    {% block tbody %}
        {% for row in nodes.rows %}
            <tr>
                {% for column, display in columns %}
                    {% if true == display %}
                        {% if 'name' == column %}
                            <td>
                                <a class="{{ theme_link_primary }}" href="{{ path('nodes_read', {'node': row['name']}) }}">{{ row['name'] }}</a>
                                {% if '*' == row['master'] %}{{ appImport.badge({'title': 'master'|trans, 'context': 'master_node'}) }}{% endif %}

                                {% if row['node.role'] is defined %}
                                    <br><small>{{ 'main_roles'|trans }}: {% for role in row['node.role']|split('') %}
                                        {% if role in ['d', 'i', 'm', 'v'] %}
                                            {{ appImport.badge({'title': ('node_roles.' ~ role)|trans, 'context': 'secondary'}) }}
                                        {% endif %}
                                    {% endfor %}</small>
                                {% endif %}

                                {% if row['host'] is defined and row['host'] != row['ip'] %}
                                    <br><small>{{ 'host'|trans }}: {{ appImport.badge({'title': row['host'], 'context': 'secondary'}) }}</small>
                                {% endif %}
                                {% if row['ip'] is defined %}
                                    <br><small>{{ 'ip'|trans }}: {{ appImport.badge({'title': row['ip'], 'context': 'secondary'}) }}</small>
                                {% endif %}
                            </td>
                        {% endif %}

                        {% if 'cpu' == column %}
                            <td class="text-nowrap">
                                {% if row['cpu'] is defined %}
                                    {{ 'usage'|trans }}: {{ appImport.badge({'title': row['cpu'] ~ '%', 'context': percentThreshold < row['cpu'] ? 'warning' : 'info'}) }}
                                    {% if row['stats']['process']['cpu']['percent'] is defined %}
                                        <br><small>{{ 'es_process'|trans }}: {{ appImport.badge({'title': row['stats']['process']['cpu']['percent'] ~ '%', 'context': percentThreshold < row['stats']['process']['cpu']['percent'] ? 'warning' : 'secondary'}) }}</small>
                                    {% endif %}
                                {% elseif row['stats']['process']['cpu']['percent'] is defined %}
                                    {{ 'es_process'|trans }}: {{ appImport.badge({'title': row['stats']['process']['cpu']['percent'] ~ '%', 'context': percentThreshold < row['stats']['process']['cpu']['percent'] ? 'warning' : 'info'}) }}
                                {% endif %}
                                {% if row['os']['allocated_processors'] is defined %}
                                    <br><small>{{ 'processors'|trans }}: {{ appImport.badge({'title': row['os']['allocated_processors'], 'context': 'secondary'}) }}</small>
                                {% endif %}
                            </td>
                        {% endif %}

                        {% if 'heap' == column %}
                            <td class="text-nowrap d-none d-xl-table-cell">
                                {{ 'usage'|trans }}: {{ appImport.badge({'title': row['heap.percent'] ~ '%', 'context': percentThreshold < row['heap.percent'] ? 'warning' : 'info'}) }}
                                {% if row['heap.current'] is defined %}
                                    <br><small>{{ 'used'|trans }}: {{ appImport.badge({'title': row['heap.current']|human_filesize, 'context': 'secondary'}) }}</small>
                                {% endif %}
                                <br><small>{{ 'total'|trans }}: {{ appImport.badge({'title': row['heap.max']|human_filesize, 'context': 'secondary'}) }}</small>
                            </td>
                        {% endif %}

                        {% if 'memory' == column %}
                            <td class="text-nowrap">
                                {{ 'usage'|trans }}: {{ appImport.badge({'title': row['ram.percent'] ~ '%', 'context': percentThreshold < row['ram.percent'] ? 'warning' : 'info'}) }}
                                {% if row['ram.current'] is defined %}
                                    <br><small>{{ 'used'|trans }}: {{ appImport.badge({'title': row['ram.current']|human_filesize, 'context': 'secondary'}) }}</small>
                                {% endif %}
                                <br><small>{{ 'total'|trans }}: {{ appImport.badge({'title': row['ram.max']|human_filesize, 'context': 'secondary'}) }}</small>
                            </td>
                        {% endif %}

                        {% if 'disk' == column %}
                            <td class="text-nowrap">
                                {% if row['disk.used_percent'] is defined %}
                                    {{ 'usage'|trans }}: {{ appImport.badge({'title': row['disk.used_percent'] ~ '%', 'context': row['disk_threshold'] is defined ? row['disk_threshold'] : 'info'}) }}
                                    <br><small>{{ 'used'|trans }}: {{ appImport.badge({'title': row['disk.used']|human_filesize, 'context': 'secondary'}) }}</small>
                                    <br><small>{{ 'total'|trans }}: {{ appImport.badge({'title': row['disk.total']|human_filesize, 'context': 'secondary'}) }}</small>
                                {% else %}
                                    {{ 'available'|trans }}: {{ appImport.badge({'title': row['disk.avail']|human_filesize, 'context': row['disk_threshold'] is defined ? row['disk_threshold'] : 'info'}) }}
                                {% endif %}
                            </td>
                        {% endif %}

                        {% if 'uptime' == column %}
                            <td class="text-nowrap d-none d-xl-table-cell">
                                {{ appImport.badge({'title': row['uptime'], 'context': 'info'}) }}
                            </td>
                        {% endif %}

                        {% if 'load_average' == column %}
                            <td class="text-nowrap">
                                {% if row['load_1m'] is defined and row['load_1m'] %}
                                    {% if row['os']['allocated_processors'] is defined %}
                                        {% set load = ((row['load_1m']*100)/row['os']['allocated_processors'])|round(2) %}
                                        1 min: {{ appImport.badge({'title': load ~ '%', 'context': percentThreshold < load ? 'warning' : 'info'}) }}
                                        {% if row['load_5m'] is defined and row['load_5m'] %}
                                            {% set load = ((row['load_5m']*100)/row['os']['allocated_processors'])|round(2) %}
                                            <br><small>5 min: {{ appImport.badge({'title': load ~ '%', 'context': percentThreshold < load ? 'strongwarning' : 'secondary'}) }}</small>
                                        {% endif %}
                                        {% if row['load_15m'] is defined and row['load_15m'] %}
                                            {% set load = ((row['load_15m']*100)/row['os']['allocated_processors'])|round(2) %}
                                            <br><small>15 min: {{ appImport.badge({'title': load ~ '%', 'context': percentThreshold < load ? 'danger' : 'secondary'}) }}</small>
                                        {% endif %}
                                    {% else %}
                                        1 min: {{ appImport.badge({'title': row['load_1m']|round(2), 'context': 'info'}) }}
                                        {% if row['load_5m'] is defined and row['load_5m'] %}
                                            <br><small>5 min: {{ appImport.badge({'title': row['load_5m']|round(2), 'context': 'secondary'}) }}</small>
                                        {% endif %}
                                        {% if row['load_15m'] is defined and row['load_15m'] %}
                                            <br><small>15 min: {{ appImport.badge({'title': row['load_15m']|round(2), 'context': 'secondary'}) }}</small></small>
                                        {% endif %}
                                    {% endif %}
                                {% endif %}
                            </td>
                        {% endif %}

                        {% if 'file_descriptors' == column %}
                            <td class="text-nowrap d-none d-xl-table-cell">
                                {{ 'usage'|trans }}: {{ appImport.badge({'title': row['file_desc.percent'] ~ '%', 'context': percentThreshold < row['file_desc.percent'] ? 'warning' : 'info'}) }}
                                {% if row['file_desc.current'] is defined %}
                                    <br><small>{{ 'used'|trans }}: {{ appImport.badge({'title': row['file_desc.current'], 'context': 'secondary'}) }}</small>
                                {% endif %}
                                <br><small>{{ 'total'|trans }}: {{ appImport.badge({'title': row['file_desc.max'], 'context': 'secondary'}) }}</small>
                            </td>
                        {% endif %}
                    {% endif %}
                {% endfor %}
            </tr>
        {% endfor %}
    {% endblock %}
{% endembed %}
